package com.mall.controller;

import com.mall.common.utils.BeanUtils;
import com.mall.domain.dto.*;
import com.mall.domain.po.Address;
import com.mall.domain.po.User;
import com.mall.domain.response.Response;
import com.mall.domain.vo.UserLoginVO;
import com.mall.enums.UserStatus;
import com.mall.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    /**
     *登录验证
     * @param loginFormDTO
     * @return
     */
    @PostMapping("login")
    public  UserLoginVO login(@RequestBody @Validated LoginFormDTO loginFormDTO){
        return userService.login(loginFormDTO);
    }

    /**
     * 扣减用户余额
     * @param pw
     * @param amount
     */
    @PutMapping("/money/deduct")
    public void deductMoney(@RequestParam("pw") String pw,@RequestParam("amount") Integer amount){
        userService.deductMoney(pw, amount);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public void register(@Validated @RequestBody RegisterUserReqDTO registerUserReqDTO) {
        log.info("收到用户注册请求：{}", registerUserReqDTO);
         userService.register(registerUserReqDTO);
    }

    /**
     * 发送验证码
     */
    @PostMapping("/verification/code")
    public void send(@Validated @RequestBody SendVerificationCodeReqDTO sendVerificationCodeReqDTO) {
        userService.send(sendVerificationCodeReqDTO);
    }

    /**
     * 获取用户信息6
     *
     */
    @GetMapping("/message")
    public Response<UserMessage> getUserMessage(){
        return userService.getUserMessage();
    }
    /**
     * 修改用户信息
     */
    @PutMapping("/message")
    public Response<?> updateMessage(String username,String phone){
        return userService.updateMessage(username,phone);
    }

    @GetMapping("/getmoney")
    public Response<Integer> getmoney(){
        return userService.getmoney();
    }

    /**
     * 批量冻结用户
     */
    @PutMapping("/batch-freeze")
    public Response<?> freeze(@RequestBody UserBatchDTO userBatchDTO){
        return userService.freeze(userBatchDTO);
    }

    /**
     * 批量删除用户
     */
    @PostMapping("/batch-delete")
    public Response<?> deleteBatch(@RequestBody UserBatchDTO userBatchDTO){
        return userService.deleteBatch(userBatchDTO);
    }

    @GetMapping("/freeze/{userId}")
    public Response<?> freezeById(@PathVariable Long userId){
        User user = userService.getById(userId);
        user.setStatus(UserStatus.FROZEN);
        userService.updateById(user);
        return Response.success();
    }

    @GetMapping("/fetchUser/{userId}")
    public Response<?> fetchUser(@PathVariable Long userId){
        User user = userService.getById(userId);
        user.setStatus(UserStatus.NORMAL);
        userService.updateById(user);
        return Response.success();
    }

    @PostMapping("/add")
    public Response<?> add(@RequestBody UserDTO userDTO){
        return userService.add(userDTO);
    }

    /**
     * 编辑用户
     */
    @PutMapping("/{id}")
    public Response<?> updateUser(
            @PathVariable("id") Long id,
            @RequestBody User2DTO user
    ) {
        if (!id.equals(user.getId())) {
            return Response.fail("路径 ID 与用户 ID 不一致，操作非法");
        }
        User user1 = userService.getById(id);
        BeanUtils.copyProperties(user,user1);
        userService.updateById(user1);
        return Response.success("编辑用户成功"); // 成功响应（可带消息）
    }
}
